'''
Description: Humanoid Robot with AI: Multi-modal AI:Speech+Vision+Gesture =Agent
Copyright (c) 2024 APS
FilePath: /Humanoid_Robot/Agent/agent_go.py
version: 
Author: Humanoid Robot Team
Date: 2024-09-13 13:55:01
LastEditors: SY
LastEditTime: 2024-5-22 15:54:51
'''

from utils_py.utils_asr import *             # 录音+语音识别
from utils_py.utils_llm import *             # 大语言模型API
from utils_py.utils_agent import *           # 智能体Agent编排
from utils_py.utils_tts import *             # 语音合成模块
import json
import ast
import threading

print('welcome to Humanoid Robot')
# play_wav('asset/welcome.wav')
# back_home()
def agent_play():
    '''
    主函数，语音控制机械臂智能体编排动作
    '''
    print('测试摄像头')
    
    # 输入指令
    start_record_ok = input('是否开启录音，按r开始录制，按k打字输入，按c输入默认指令')
    if start_record_ok == 'r':
        record_auto()
        # voice_transfer()
        # speech_recognition()
        # # 使用线程进行录音和转换，以便并行处理
        # record_thread = threading.Thread(target=record_auto)
        # record_thread.start()
        # record_thread.join()  # 等待录音完成

        # 使用异步或多线程进行音频转换和语音识别
        # def process_audio():
        #     voice_transfer()
        #     return speech_recognition()

        # # 启动音频处理线程
        # audio_thread = threading.Thread(target=process_audio)
        # audio_thread.start()
        # audio_thread.join()  # 等待音频处理完成
        voice_transfer()
        order = speech_recognition()

        # order = process_audio()  # 获取识别结果
    elif start_record_ok == 'k':
        order = input('请输入指令')
    elif start_record_ok == 'c':
        order = '先回到原点，再移动到拍照位'
    else:
        print('无效输入，请重试。')
        return

    result = agent_plan(order)
    print(result)
    
    try:
        agent_plan_output = ast.literal_eval(result)
    except (ValueError, SyntaxError) as e:
        print(f'解析结果出错: {e}')
        return
    
    print('智能体编排动作如下\n', agent_plan_output)
    
    # 直接执行动作和语音播放逻辑
    response = agent_plan_output.get('response', '')
    
    # 启动语音播放线程
    if response:
        tts_speak = threading.Thread(target=tts_play_wav, args=(response, 'temp/tts.wav'))
        tts_speak.start()
    
    # 定义并启动动作执行线程
    def execute_actions():
        for each in agent_plan_output.get('function', []):
            print('开始执行动作', each)
            try:
                eval(each)
            except Exception as e:
                print(f'执行动作 {each} 时出错: {e}')
    
    action_thread = threading.Thread(target=execute_actions)
    action_thread.start()
    
    # 如果需要等待两个线程都完成，可以使用 join
    tts_speak.join()
    action_thread.join()

if __name__ == '__main__':
    while(1):
        agent_play()
